package com.cbs.android.function.datepick;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cbs.android.component.application.activity.CBSDialogFragment;
import com.cbs.android.widget.calendar.KCalendar;

/**
 * Created by nijie on 2016/8/5.
 */
public class CalendarDialogFragment extends CBSDialogFragment {

	public String date;
	private DateSelectListener dateSelectListener;

	public CalendarDialogFragment setDate(String date) {
		this.date = date;
		return this;
	}

	public CalendarDialogFragment setDateSelectListener(DateSelectListener dateSelectListener) {
		this.dateSelectListener = dateSelectListener;
		return this;
	}

	public CalendarDialogFragment setCancelForbidden() {
		this.setCancelable(false);
		return this;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setStyle(STYLE_NO_TITLE, com.cbs.android.component.application.R.style.MyDialogFrame);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);
		View view = inflater.inflate(R.layout.cbs_dialog_calendar, container, false);

		final TextView popupwindow_calendar_year = (TextView) view.findViewById(R.id.popupwindow_calendar_year);
		final TextView popupwindow_calendar_month = (TextView) view.findViewById(R.id.popupwindow_calendar_month);
		final KCalendar calendar = (KCalendar) view.findViewById(R.id.popupwindow_calendar);
		Button popupwindow_calendar_bt_enter = (Button) view.findViewById(R.id.popupwindow_calendar_bt_enter);

		popupwindow_calendar_month.setText(calendar.getCalendarMonth() + "月");
		popupwindow_calendar_year.setText(calendar.getCalendarYear() + "年");
		if (null != date) {
			int years = Integer.parseInt(date.substring(0,
					date.indexOf("-")));
			int month = Integer.parseInt(date.substring(
					date.indexOf("-") + 1, date.lastIndexOf("-")));
			popupwindow_calendar_month.setText(month + "月");
			popupwindow_calendar_year.setText(years + "年");

			calendar.showCalendar(years, month);
			calendar.setCalendarDayBgColor(date, R.drawable.cbs_calendar_background_tv);
		}

		//监听所选中的日期
		calendar.setOnCalendarClickListener(new KCalendar.OnCalendarClickListener() {

			public void onCalendarClick(int row, int col, String dateFormat) {
				int month = Integer.parseInt(dateFormat.substring(
						dateFormat.indexOf("-") + 1,
						dateFormat.lastIndexOf("-")));

				if (calendar.getCalendarMonth() - month == 1//跨年跳转
						|| calendar.getCalendarMonth() - month == -11) {
					calendar.lastMonth();

				} else if (month - calendar.getCalendarMonth() == 1 //跨年跳转
						|| month - calendar.getCalendarMonth() == -11) {
					calendar.nextMonth();

				} else {
					calendar.removeAllBgColor();
					calendar.setCalendarDayBgColor(dateFormat, R.drawable.cbs_calendar_background_tv);
					date = dateFormat;//最后返回给全局 date

					dismiss();
					if (dateSelectListener != null) {
						dateSelectListener.onSelect(date);
					}
				}
			}
		});

		//监听当前月份
		calendar.setOnCalendarDateChangedListener(new KCalendar.OnCalendarDateChangedListener() {
			public void onCalendarDateChanged(int year, int month) {
				popupwindow_calendar_month.setText(month + "月");
				popupwindow_calendar_year.setText(year + "年");
			}
		});

		//上月监听按钮
		RelativeLayout popupwindow_calendar_last_month = (RelativeLayout) view
				.findViewById(R.id.popupwindow_calendar_last_month);
		popupwindow_calendar_last_month
				.setOnClickListener(new View.OnClickListener() {

					public void onClick(View v) {
						calendar.lastMonth();
					}

				});

		//下月监听按钮
		RelativeLayout popupwindow_calendar_next_month = (RelativeLayout) view
				.findViewById(R.id.popupwindow_calendar_next_month);
		popupwindow_calendar_next_month
				.setOnClickListener(new View.OnClickListener() {

					public void onClick(View v) {
						calendar.nextMonth();
					}
				});

		//上年监听按钮
		RelativeLayout popupwindow_calendar_last_year = (RelativeLayout) view.findViewById(R.id.popupwindow_calendar_last_year);
		popupwindow_calendar_last_year.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				popupwindow_calendar_year.setText((calendar.getCalendarYear() - 1) + "年");
				calendar.showCalendar(calendar.getCalendarYear() - 1, calendar.getCalendarMonth());
			}

		});

		//下年监听按钮
		RelativeLayout popupwindow_calendar_next_year = (RelativeLayout) view.findViewById(R.id.popupwindow_calendar_next_year);
		popupwindow_calendar_next_year.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				popupwindow_calendar_year.setText((calendar.getCalendarYear() + 1) + "年");
				calendar.showCalendar(calendar.getCalendarYear() + 1, calendar.getCalendarMonth());
			}
		});

		//关闭窗口
		popupwindow_calendar_bt_enter
				.setOnClickListener(new View.OnClickListener() {

					public void onClick(View v) {
						dismiss();
					}
				});
		return view;
	}


	public interface DateSelectListener {
		void onSelect(String date);
	}


}
